/** 工具类方法集合 */
import md5 from 'js-md5'
import wxShare from './commonShare'

function signed_crc_table() {
	var c = 0, table = new Array(256);

	for(var n =0; n != 256; ++n){
		c = n;
		c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
		c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
		c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
		c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
		c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
		c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
		c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
		c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
		table[n] = c;
	}

	return typeof Int32Array !== 'undefined' ? new Int32Array(table) : table;
}
var T = signed_crc_table();
function crc32_str(str, seed) {
  var C = seed ^ -1;
  for(var i = 0, L=str.length, c, d; i < L;) {
    c = str.charCodeAt(i++);
    if(c < 0x80) {
      C = (C>>>8) ^ T[(C ^ c)&0xFF];
    } else if(c < 0x800) {
      C = (C>>>8) ^ T[(C ^ (192|((c>>6)&31)))&0xFF];
      C = (C>>>8) ^ T[(C ^ (128|(c&63)))&0xFF];
    } else if(c >= 0xD800 && c < 0xE000) {
      c = (c&1023)+64; d = str.charCodeAt(i++)&1023;
      C = (C>>>8) ^ T[(C ^ (240|((c>>8)&7)))&0xFF];
      C = (C>>>8) ^ T[(C ^ (128|((c>>2)&63)))&0xFF];
      C = (C>>>8) ^ T[(C ^ (128|((d>>6)&15)|((c&3)<<4)))&0xFF];
      C = (C>>>8) ^ T[(C ^ (128|(d&63)))&0xFF];
    } else {
      C = (C>>>8) ^ T[(C ^ (224|((c>>12)&15)))&0xFF];
      C = (C>>>8) ^ T[(C ^ (128|((c>>6)&63)))&0xFF];
      C = (C>>>8) ^ T[(C ^ (128|(c&63)))&0xFF];
    }
  }
  return C ^ -1;
}

/** 生成加密后的房间号 */
export function getRoomID (roomname, roompwd) {
  let str = roomname + '' + roompwd
  let newStr = md5(str) + crc32_str(md5(str))
  return newStr
}

/** 
 * 生成随机数
 * @param {string} len 随机数长度
 */
export function getRandomString (len) {
  len = len || 32;
  var $chars = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678"; // 默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1
  var maxPos = $chars.length;
  var pwd = "";
  for (var i = 0; i < len; i++) {
    pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
  }
  return pwd;
}

/** 
 * 格式化时间 
 * @param {string} fmt 时间格式
 * @param {Date} date date对象
 */
export function dateFormat(fmt, date) {
  let ret;
  const opt = {
    "Y+": date.getFullYear().toString(),        // 年
    "m+": (date.getMonth() + 1).toString(),     // 月
    "d+": date.getDate().toString(),            // 日
    "H+": date.getHours().toString(),           // 时
    "M+": date.getMinutes().toString(),         // 分
    "S+": date.getSeconds().toString()          // 秒
  };
  for (let k in opt) {
    ret = new RegExp("(" + k + ")").exec(fmt);
    if (ret) {
      fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
    };
  };
  return fmt;
}

/**
 * 获取全局变量名称
 * @param {string} name 全局变量的名字
 */
 export function getGlobalSetting (name) {
  if (window) {
    if (!window.__globalSetting) {
      window.__globalSetting = {}
    }
    if (!window.__globalSetting[name]) {
      window.__globalSetting[name] = JSON.parse(localStorage.getItem(name))
    }
    return window.__globalSetting[name]
  }
  return JSON.parse(localStorage.getItem(name))
}

/**
 * 设置全局变量
 * @param {string} name 变量名称
 * @param {any} value 变量值
 * @param {any} onlyCache 是否仅缓存，仅缓存的话只存在于window对象上，否则还会存于localStorage上。
 */
export function setGlobalSetting (name, value, onlyCache) {
  if (!onlyCache) {
    localStorage.setItem(name, JSON.stringify(value))
  }
  if (window) {
    if (!window.__globalSetting) {
      window.__globalSetting = {}
    }
    window.__globalSetting[name] = value
  }
}

/**
 * 删除全局变量
 * @param {string} name
 */
export function removeGlobalSetting (name) {
  if (window && window.__globalSetting) {
    delete window.__globalSetting[name]
  }
  localStorage.removeItem(name)
}

/** 微信分享 */
export function handleWXShare (obj) {
    this.$store.state.functionMap.isWx && wxShare.commonShare(obj)
}
